package com.longxi.tianjintianbao.ui.adapter.multitype;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.listener.SelectRectificationItemListener;
import com.longxi.tianjintianbao.model.rectification.RectificationItem;

import me.drakeet.multitype.ItemViewBinder;

/**
 * Created by cxw on 2018/2/20.
 */
public class RectificationItemViewBinder extends ItemViewBinder<RectificationItem, RectificationItemViewBinder.ViewHolder> {

    SelectRectificationItemListener listener;

    public RectificationItemViewBinder(SelectRectificationItemListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_rectification_item, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull RectificationItem rectificationItem) {
        if(!"".equals(rectificationItem.getState())){
            SpannableStringBuilder builder = new SpannableStringBuilder("整改进度: " + rectificationItem.getState());
            builder.setSpan(holder.redSpan, 6, builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            holder.mTvState.setText(builder);
        }

        holder.mTvItemName.setText("项目名称: " + rectificationItem.getItemname());
        holder.mTvScore.setText(rectificationItem.getScore() + "/" + rectificationItem.getAllscore() + "分");
        if(!"".equals(rectificationItem.getContent())){
            holder.mTvContent.setText("整改内容: " + rectificationItem.getContent());
        }
        holder.mLlBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onSuccess(rectificationItem);
            }
        });
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout mLlBtn;
        TextView mTvState;
        TextView mTvItemName;
        TextView mTvScore;
        TextView mTvContent;
        ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);
        ViewHolder(View itemView) {
            super(itemView);
            mLlBtn = itemView.findViewById(R.id.llBtn);
            mTvState = itemView.findViewById(R.id.tvState);
            mTvItemName = itemView.findViewById(R.id.tvItemName);
            mTvScore = itemView.findViewById(R.id.tvScore);
            mTvContent = itemView.findViewById(R.id.tvContent);
        }
    }
}
